package org.lanqiao.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.lanqiao.entity.Service;
import org.lanqiao.service.ServiceService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/service")
public class ServiceController {
	@Resource
	private ServiceService serviceService;
	
	//模糊查询并分页显示业务列表
	@RequestMapping("/showServicelist")
	public ModelAndView showServicelist(Service service1,HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("service/service_list");
		
		service1.setSosname(request.getParameter("sosname"));
		service1.setSip(request.getParameter("sip"));
		service1.setSidcard(request.getParameter("sidcard"));
		service1.setSstatus(request.getParameter("sstatus"));
		
		int totalRecord = serviceService.selectTotal(service1);
		int pageSize  =  5;
		int currentPage = -1;
		String curPage = request.getParameter("currentPage");
		if(curPage==null)
			currentPage=1;
		else
			currentPage=Integer.parseInt(curPage);  
		int pageCount = (totalRecord%pageSize==0)?(totalRecord/pageSize):(totalRecord/pageSize+1);
        int a = (currentPage-1)*pageSize;
        mav.addObject("totalRecord", totalRecord);
        mav.addObject("pageSize", pageSize);
        mav.addObject("currentPage", currentPage);
        mav.addObject("pageCount", pageCount);
        mav.addObject("a", a);
		service1.setA(a);
		List<Service> service = serviceService.selectFenye(service1);
		mav.addObject("service", service);
		mav.addObject("sservice", service1);
		return mav;
	}
	
	//显示添加业务界面
	@RequestMapping("/showServiceadd")
	public String showServiceadd() {
		return "service/service_add";
	}
	
	//实现业务添加功能
	@RequestMapping("/serviceadd")
	public String serviceadd(Service service,HttpServletRequest request){
		boolean resultService = serviceService.insertService(service);
		if(!resultService){
			request.setAttribute("flag1",0);
			request.setAttribute("service", service);//回显数据
		}else{
			request.setAttribute("flag1",1);			
		}
		return "service/service_add";
	}
	
	//显示修改业务界面
	@RequestMapping("/showServiceupdate")
	public String showServiceupdate(HttpServletRequest request) {
		String id = request.getParameter("sid");
		int sid = Integer.parseInt(id);
		Service service = serviceService.selectUpdate(sid);
		request.setAttribute("service", service);
		return "service/service_update";
	}
	
	//实现修改业务功能
	@RequestMapping("/serviceupdate")
	public String serviceupdate(Service service,HttpServletRequest request) {
		boolean a = serviceService.updateService(service);
		if(!a){
			request.setAttribute("flag1",0);
			request.setAttribute("service", service);//回显数据
		}else{
			request.setAttribute("flag1",1);
		}
		return "service/service_update";
	}
	
	//显示业务细节页面
	@RequestMapping("/showServicedetail")
	public String showServicedetail(HttpServletRequest request) {
		String id = request.getParameter("sid");
		int sid = Integer.parseInt(id);
		Service service = serviceService.selectUpdate(sid);
		request.setAttribute("service", service);
		return "service/service_detail";
	}
	
	//实现删除按钮功能
	@RequestMapping("/servicedelete")
	public String servicedelete(HttpServletRequest request) {
		String id = request.getParameter("sid");
		int sid = Integer.parseInt(id);
		boolean a = serviceService.updateDelete(sid);
		if(!a){
			request.setAttribute("flag1",0);
		}else{
			request.setAttribute("flag1",1);
		}
		return "forward:showServicelist.do";
	}
	
	//实现开通按钮功能
	@RequestMapping("/servicestart")
	public String servicestart(HttpServletRequest request) {
		String id = request.getParameter("sid");
		int sid = Integer.parseInt(id);
		boolean a = serviceService.updateStart(sid);
		if(!a){
			request.setAttribute("flag1",00);
		}else{
			request.setAttribute("flag1",11);
		}
		return "forward:showServicelist.do";
	}
	
	//实现暂停按钮功能
	@RequestMapping("/servicestop")
	public String servicestop(HttpServletRequest request) {
		String id = request.getParameter("sid");
		int sid = Integer.parseInt(id);
		boolean a = serviceService.updateStop(sid);
		if(!a){
			request.setAttribute("flag1",000);
		}else{
			request.setAttribute("flag1",111);
		}
		return "forward:showServicelist.do";
	}
}